﻿using System;
using System.Web;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Ref;

namespace Event
{
    public partial class Upload : System.Web.UI.Page
    {
        protected string _cancelUrl;

        protected void Page_Load(object sender, EventArgs e)
        {
            var email = string.Empty;
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                var ident = HttpContext.Current.User.Identity as FormsIdentity;
                if (ident != null)
                {
                    try
                    {
                        var ticket1 = ident.Ticket;
                        var userData = ticket1.UserData.Split('|');
                        email = userData[1];
                    }
                    catch (Exception)
                    {
                        Response.Redirect("/");
                    }
                }
                else
                {
                    Response.Redirect("/");
                }
            }
            else if (HttpContext.Current.Session[Const.AuthenticationSessionName] != null)
            {
                string[] userData = HttpContext.Current.Session[Const.AuthenticationSessionName].ToString().Split('|');
                email = userData[1];
            }
            else
            {
                Response.Redirect("/");
            }

            if (!IsPostBack && !string.IsNullOrEmpty(email))
            {
                InitData();
                var o = MembersCache.Members_GetByEmail(email);
                if (o.Member_Status == (int)MemberStatus.IsPublished)
                {
                    if (!RefCache.Ref_GetLockEditStatus() || o.Member_InRound < RefCache.Ref_GetCurrentRound()) Response.Redirect("/");
                }
                else if (o.Member_Status == (int)MemberStatus.IsNoRegister)
                {
                    if (!RefCache.Ref_GetLockRegisterStatus()) Response.Redirect("/");
                }
                else Response.Redirect("/");
                BindData(o);
                MetaKeyword.AddMetaTag(Page, Const.Meta_Title, Const.Meta_Sapo);
                MetaKeyword.AddFacebookMetaTag(Page, Const.Meta_Title, Const.Meta_Sapo, Const.SITE_URL, Const.Meta_Image);
            }

        }

        private void BindData(MembersObject o)
        {
            txtFullName.Text = o.Member_FullName;
            //txtEmail.Text = string.IsNullOrWhiteSpace(o.Member_ContactEmail) ? o.Member_Email : o.Member_ContactEmail;
            txtAddress.Text = o.Member_Address;
            txtPhone.Text = o.Member_Tel;
            ddlDay.SelectedValue = o.Member_Birthday.Day.ToString();
            ddlMonth.SelectedValue = o.Member_Birthday.Month.ToString();
            ddlYear.SelectedValue = o.Member_Birthday.Year.ToString();
            ddlGender.SelectedValue = o.Member_Gender.ToString();
            txtAbout.Text = o.Member_About;
            var albumImage = string.Empty;
            switch (o.Member_InRound)
            {
                case (int)MemberRound.Round2:
                    txtTitle.Text = o.Member_AlbumTitle2;
                    txtDesc.Text = o.Member_AlbumDesc2;
                    albumImage = o.Member_OriginalAvatar2;
                    break;
                case (int)MemberRound.Round3:
                    txtTitle.Text = o.Member_AlbumTitle3;
                    txtDesc.Text = o.Member_AlbumDesc3;
                    albumImage = o.Member_OriginalAvatar3;
                    break;
                default:
                    txtTitle.Text = o.Member_AlbumTitle1;
                    txtDesc.Text = o.Member_AlbumDesc1;
                    albumImage = o.Member_OriginalAvatar;
                    break;

            }
            if (o.Member_Status == (int)MemberStatus.IsPublished) userAvatar.Src = Const.STORAGE_DOMAIN + o.Member_Avatar;
            if (o.Member_Status == (int)MemberStatus.IsPublished) albumAvatar.Src = string.IsNullOrWhiteSpace(albumImage) ? Const.NO_AVATAR_ALBUM : Const.STORAGE_DOMAIN + albumImage;
            _cancelUrl = o.Member_Status == (int) MemberStatus.IsPublished
                             ? UrlHelpers.MembersDetails(o.Member_ID, o.Member_Email, o.Member_InRound)
                             : "/";
        }


        private void InitData()
        {
            var lstCity = MembersCache.City_GetAllFrontEnd();
            ddlCity.DataSource = lstCity;
            ddlCity.DataValueField = Constants.Table_City.City_ID;
            ddlCity.DataTextField = Constants.Table_City.City_Name;
            ddlCity.DataBind();

            ddlYear.DataSource = DateTimeFunctions.GetListYear();
            ddlYear.DataValueField = "Key";
            ddlYear.DataTextField = "Value";
            ddlYear.DataBind();
            ddlYear.SelectedValue = "1989";

            ddlMonth.DataSource = DateTimeFunctions.GetListMonth();
            ddlMonth.DataValueField = "Key";
            ddlMonth.DataTextField = "Value";
            ddlMonth.DataBind();

            ddlDay.DataSource = DateTimeFunctions.GetListDay();
            ddlDay.DataValueField = "Key";
            ddlDay.DataTextField = "Value";
            ddlDay.DataBind();
        }
    }
}